/**
 * 
 */
package org.cuatrovientos.erp.controller;


import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.BOGenericInterface;
import org.cuatrovientos.erp.business.StockBO;
import org.cuatrovientos.erp.model.Stock;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Stock management
 * @author Enrico Martinez
 *
 */
@Controller
public class StockController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(StockController.class);

	@Autowired
    private StockBO stockBO;
    
        /**
         * default constructor
         */
        public StockController () {
               logger.info("Stock BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /stock or /stock/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/stock","/stock/list"})
         */
        @RequestMapping({"/stock","/stock/list"})
        public ModelAndView stockDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Stock> stockList = stockBO.selectAll();
                model.put("stocks", stockList );
                return new ModelAndView("StockList", model);     
        }



        /**
         * handles delete stock, and redirect to stock default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/stock","/stock/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/stock/delete"})
        public String deleteStock(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            stockBO.deleteById(id);
            return "redirect:/stock";

        }
        
        /**
         * handles new stock request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "stock"
         * @return the name of the view to show
         * @RequestMapping({"/stock/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/stock/new"})
        public ModelAndView newStock() {
        		logger.info("new stock ");
                Stock stock = new Stock();
               
                return new ModelAndView("NewStock", "stock", stock);
        }
        
        /**
         * handles new stock request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/stock/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/stock/save"})
        public String newStock(@Valid Stock stock, BindingResult bindingResult) {
        		logger.info("new stock " + stock.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("stock", stock);
        			return "NewStock";
        		}
        		
        		// If data is ok, insert and go on
                stockBO.insert(stock);
                return "redirect:/stock";
        }
        
        /**
         * handles delete stock, and redirect to stock default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/stock","/stock/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/stock/detail"})
        public ModelAndView stockDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Object stock = stockBO.selectById(id);
            model.put("stock", stock);
            
            return new ModelAndView("StockDetail",model);

        }
        
        /**
         * handles stock update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/stock/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/stock/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Object stock = stockBO.selectById(id);
                model.put("stock", stock);
               return new ModelAndView("UpdateStock",model);

        }
        
        /**
         * handles save update stock request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/stock/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/stock/saveupdate"})
        public String saveUpdate(@Valid Stock stock, BindingResult bindingResult) {
        		logger.info("update stock " + stock.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("stock", stock);
        			return "UpdateStock";
        		}
        		
                stockBO.update(stock);
                return "redirect:/stock";
        }
}
